﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MagicArena.framework.logic;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using MagicArena.framework.state;
using MagicArena.framework.action;
using MagicArena.framework.toon;

namespace MagicArena.impl.logic
{
    public class PlayerLogic : AToonLogic
    {
        #region Properties
        public PlayerIndex Index
        {
            get;
            private set;
        }
        #endregion

        #region Private vars
        private const float MinMove = 1;
        private State MoveDown = new State(State.Common.Ypos, -1 * MinMove);
        private State MoveUp = new State(State.Common.Ypos, MinMove);
        private State MoveLeft = new State(State.Common.Xpos, -1 * MinMove);
        private State MoveRight = new State(State.Common.Xpos, MinMove);
        #endregion

        public PlayerLogic(PlayerIndex index)
        {
            Index = index;
        }

        public override void NextAction(Toon[] targets, out framework.action.AAction act, out framework.toon.Toon target)
        {
            // Target first
            target = targets[0];
            act = null;
            //Get gamepad state
            GamePadState state = GamePad.GetState(Index);
            State moveState=null;
            if (state.DPad.Down == ButtonState.Pressed)
            {
                moveState = MoveDown;
            }
            else if (state.DPad.Up == ButtonState.Pressed)
            {
                moveState = MoveUp;
            }
            else if (state.DPad.Left == ButtonState.Pressed)
            {
                moveState = MoveLeft;
            }
            else if (state.DPad.Right == ButtonState.Pressed)
            {
                moveState = MoveRight;
            }

            // Find an action which modified the corresponding move state\
            if (moveState != null)
            {
                State s;
                foreach (AAction a in this.Actions)
                {
                    s = State.FindState(a.StateChanges, moveState);
                    if (s != null)
                    {
                        act = a;
                        break;
                    }
                }
            }


        }

        public override void Update(GameTime gameTime)
        {
            //TODO Does anything need done here?
        }
    }
}
